package com.twu.biblioteca.dao.memory;

import com.google.common.collect.Lists;

import com.twu.biblioteca.dao.UserDao;
import com.twu.biblioteca.entity.User;
import com.twu.biblioteca.exception.DaoNotFoundException;

import java.util.List;

public class MemoryUserDao extends MemoryBaseDao<User> implements UserDao {

  public MemoryUserDao() {
    List<User> userList = Lists.newArrayList();
    User user1 = new User("Daniel", "111-1111");
    user1.setEmail("daniel@biblioteca.org");
    user1.setAddress("4/44 any Street Pune");
    user1.setPhoneNumber("111-1111-1111");
    user1.setPassword("password");

    User user2 = new User("Esther", "222-2222");
    user2.setEmail("esther@biblioteca.org");
    user2.setAddress("4/44 any Street Pune");
    user2.setPhoneNumber("111-1111-1111");
    user2.setPassword("password2");

    userList.add(user1);
    userList.add(user2);
    super.setItemList(userList);
  }

  @Override
  public User findWithLibraryNumber(String libraryNumber) {
    boolean matches = libraryNumber.matches("\\w{3}-\\w{4}");
    if (!matches)
      throw new IllegalArgumentException("libraryNumber shall be XXX-XXXX");

    for (User u : itemList) {
      if (u.getLibraryNumber().equals(libraryNumber))
        return u;
    }
    throw new DaoNotFoundException("No item has found");
  }
}
